<?php
include('../../include/functions.inc.php');
$user = unserialize($_SESSION['user']);
//$fac = $_SESSION["factura"];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Asignacion de Estudiantes</title>
        <link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
        <link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
        <link href="../../js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />
        <link href="../../js/protocalendar/datepicker.css" rel="stylesheet" type="text/css" />
        <script src="../../js/jquery.js" type="text/javascript"></script>
        <script>jQuery.noConflict();</script>
        <script src="../../bootstrap/js/bootstrap.js"></script>
        <script src="../../js/prototype.js" type="text/javascript"></script>
        <script src="../../js/jsvalidate.js" type="text/javascript"></script>
        <script src="../../js/TableOrderer.js" type="text/javascript"></script>
        <script src="../../js/dialog/dialog.2.0.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/prototype-date-extensions.js" type="text/javascript"></script>
        <script src="../../js/protocalendar/datepicker.js"  type="text/javascript"></script>
        <script src="../../js/include.js" type="text/javascript"></script>
        <style type="text/css">
            input,
            textarea{
                width: 300px;
            }
            select{
                width: 314px;
            }
        </style>
        <script type="text/javascript">
            var cargos=new Hash();
            var selected=null;
            setCargo=function(){
                if($F("FECHAPAGO").empty()){
                    Dialogs.alert("Tiene que ingresar la fecha de pago del cargo!!!");
                    return;
                }
                var cargo=new Hash({ID:$F("CD"),NOMBRE:$F("NOMBRE"),
                    MONTO:parseFloat(cleanComa($F("MONTO"))),MORA:parseFloat(cleanComa($F("MORA"))),
                    FECHA_PAGO:$F("FECHAPAGO"),MONEDA:$F("MONEDA")});
                cargo=cargo.toObject();
                cargos.set(cargo.ID,cargo);
                refreshTable();
                selected=null;
                $("ITEM").reset();
            }

            refreshTable=function(){
                new TableOrderer("detalle",{data:cargos.values(),
                    filter:'top',callBack:loadCargo});
            }
            cleanComa=function(str){
                n=str.split(",");

                newStr="";
                var i=0;
                for(i=0;i<n.length;i++){
                    newStr=newStr+n[i];
                }
                return newStr;
            }
            loadCargo=function(s){
                $("NOMBRE").value=s.NOMBRE;
                $("MONTO").value=cleanComa(s.MONTO);
                $("MORA").value=cleanComa(s.MORA);
                $("CD").value=s.ID;
                $("FECHAPAGO").value=s.FECHA_PAGO;
                $("MONEDA").value=s.MONEDA;
            }

            delCargo=function(){
                cargos.unset($F("CD"));
                refreshTable();
                $("ITEM").reset();
            }

            var rsv;
            config=function(){
                rsv=new RSV({
                    formID: $(document.CARGOS),
                    errorFieldClass: "errorField",
                    rules: ["required,ID,Debe seleccionar un estudiante!"],
                    displayType : "display-html",
                    errorHTMLItemBullet : "&#8212; ",
                    onCompleteHandler:function(){
                        if(!confirm("Los cargos se asociaran a la asignacion activa más reciente del alumno. En caso no haya, no se asociarán. ¿Desea continuar?")){
                            return false;
                        };
                        if(cargos.values().length==0){
                            Dialogs.alert('No hay cargos que guardar!');
                        }else{
                            $('operation').value='save';
                            $('json').value=JSON.stringify(cargos.values());

                            waitMsg("Guardando...");
                            $(document.CARGOS).request({onComplete:function(r){
                                    // alert(r.responseText);
                                    closeWait();
                                    var msg='Numero de Cargos: '+r.responseJSON.NO_CARGOS;
                                    msg=msg+'<HR>Total Cargos: '+r.responseJSON.TOTAL;
                                    waitMsg(msg);
                                    $(document.CARGOS).action="index.php";
                                    $(document.CARGOS).submit();
                                }});
                        }
                    }
                });
            }
            addOnLoad(config);
            showCargos=function(){
                var f=new Hash({tittle:'Cargos Facturacion',width:500,height:500,url:'data.php?operation=cargos',callback:selCargo});
                setList(null,f.toObject());
            }
            showStudents=function(){
                var f=new Hash({tittle:'Listado de Alumnos',width:500,height:500,url:'data.php?operation=stud',callback:selStudent});
                setList(null,f.toObject());
            }

            selCargo=function(s){
                selected=s;
                $("NOMBRE").value=s.NOMBRE;
                $("MONTO").value=s.MONTO;
                $("MORA").value=s.MORA;
                $("CD").value=s.ID;
	
                closeWait();
            }

            selStudent=function(s){
                $("ID").value=s.CARNET;
                $("AIU_CODE").value=s.AIU_CODE;
                $("NAME").value=s.NOMBRE_A+' '+s.NOMBRE_B+' '+s.APELLIDOS;
                closeWait();
            }
        </script>
    </head>

    <body>
        <div id="tabs">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a>Asignación Manual de Cargos</a>
                </li>
            </ul>
        </div>
        <div id="tabs-cont">
            <div id="rsvErrors"></div>

            <table class=""  width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <th scope="col" width="50%"></th>    
                    <th  width="50%" valign="middle" scope="col" ><input type="button" name="cmdOk2" class="btn btn-info" id="cmdOk2" value="Continuar" onclick="rsv.validate();" /></th>
                </tr>
                <tr>
                    <td colspan="2">
                        <form id="CARGOS" name="CARGOS" method="GET" action="data.php">
                            <br/>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td width="20%"> NO. CARNET</td>
                                    <td width="80%"><input name="ID" type="text" id="ID" readonly="readonly" />
                                        <input name="lststudent" type="button" class="btn btn-small" id="lststudent" onclick="javascript:showStudents()"  value="..." /></td>
                                </tr>
                                <tr>
                                    <td>AIU CODIGO</td>
                                    <td><input name="AIU_CODE" type="text" id="AIU_CODE" readonly="readonly" /></td>
                                </tr>
                                <tr>
                                    <td>NOMBRE ESTUDIANTE</td>
                                    <td><input name="NAME" type="text" id="NAME" readonly="readonly" size="50" />
                                        <input type="hidden" name="operation" id="operation" />
                                        <input type="hidden" name="json" id="json" /></td>
                                </tr>
                            </table>
                        </form></td>
                </tr>
                <tr>
                    <th colspan="2"><legend>Nuevo Cargo</legend></th>
                </tr>
                <tr>
                    <td colspan="2"> <form id="ITEM" name="ITEM" method="post" action="">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td width="20%">CARGO</td>
                                    <td width="80%"><input name="NOMBRE" type="text" id="NOMBRE" size="50" readonly="readonly" />
                                        <input type="button" name="lstcargos" onclick="javascript:showCargos()" id="lstcargos" class="btn btn-small" value="..." /></td>
                                </tr>

                                <tr>
                                    <td>MONTO</td>
                                    <td><input name="MONTO" type="text" id="MONTO" value="0.00" size="25" /></td>
                                </tr>
                                <tr>
                                    <td>MORA</td>
                                    <td><input name="MORA" type="text" id="MORA" value="0.00" size="25"/>            </td>
                                </tr>
                                <tr>
                                    <td >FECHA PAGO</td>
                                    <td ><input name="FECHAPAGO" type="text" id="FECHAPAGO" size="25" readonly="readonly" />
                                        <script type="text/javascript">
                                            new Control.DatePicker('FECHAPAGO', {icon: '../../js/protocalendar/calendar.png',dateFormat: 'yyyy-MM-dd'});
                                        </script>
                                        <input type="hidden" name="CD" id="CD" /></td>
                                </tr>
                                <tr>
                                    <td >MONEDA</td>
                                    <td ><select name="MONEDA" id="MONEDA">         
                                            <option value="1">Quetzales</option>
                                            <option value="2">Dolares</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td ></td>
                                    <td ><div class="btn-group"><input type="button" class="btn btn-info" name="cmdOk" id="cmdOk" value="Agregar" onclick="setCargo()" />
                                        <input type="button" class="btn btn-info" name="cmdKo" id="cmdKo" value="Borrar" onclick="delCargo()" /></div></td>
                                </tr>
                            </table>

                        </form>      </td>
                </tr>
                <tr>
                    <th colspan="2"><legend>Listado de Cargos a Realizar</legend></th>
                </tr>
                <tr>
                    <td colspan="2"><div id="detalle"></div></td>
                </tr>
            </table>
        </div>
    </body>
</html>
